//
//  XSVideoCutoutView.m
//  AudioVideo
//
//  Created by xueshan1 on 2019/8/26.
//  Copyright © 2019 xueshan1. All rights reserved.
//

#import "XSVideoCutoutView.h"

#define DragViewWidth 15

@interface XSVideoCutoutView ()

@property(nonatomic,strong)UIImageView *leftDragView;
@property(nonatomic,strong)UIImageView *rightDragView;
@property(nonatomic,strong)UIView *leftMaskView;
@property(nonatomic,strong)UIView *rightMaskView;
@property(nonatomic,strong)UIView *topLine;
@property(nonatomic,strong)UIView *botLine;


@end

@implementation XSVideoCutoutView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        [self setupUI];
        _leftRightInset = DragViewWidth;
    }
    return self;
}

- (void)setupUI{
    self.leftDragView.frame = CGRectMake(0, 0, DragViewWidth, self.height);
    self.rightDragView.frame = CGRectMake(self.width - DragViewWidth, 0, DragViewWidth, self.height);
    
    self.leftMaskView.frame = CGRectMake(DragViewWidth, 0, 0, self.height);
    self.rightMaskView.frame = CGRectMake(self.width - DragViewWidth, 0, 0, self.height);
    self.clipsToBounds = YES;

    self.topLine.frame = CGRectMake(0, 0, self.width, 3);
    self.botLine.frame = CGRectMake(0, self.height - 3, self.width, 3);
    
    
    UIPanGestureRecognizer *leftPan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(leftPanGesture:)];
    [self.leftDragView addGestureRecognizer:leftPan];
    UIPanGestureRecognizer *rightPan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(rightPanGesture:)];
    [self.rightDragView addGestureRecognizer:rightPan];
 
}

- (UIImageView *)leftDragView{
    if (!_leftDragView) {
        _leftDragView = [[UIImageView alloc]init];
        _leftDragView.image = [UIImage imageNamed:@"drag.jpg"];
        _leftDragView.userInteractionEnabled = YES;
        [self addSubview:_leftDragView];
    }
    return _leftDragView;
}
- (UIImageView *)rightDragView{
    if (!_rightDragView) {
        _rightDragView = [[UIImageView alloc]init];
        _rightDragView.image = [UIImage imageNamed:@"drag.jpg"];
        _rightDragView.userInteractionEnabled = YES;
        [self addSubview:_rightDragView];
    }
    return _rightDragView;
}
- (UIView *)leftMaskView{
    if (!_leftMaskView) {
        _leftMaskView = [[UIView alloc]init];
        _leftMaskView.backgroundColor = [UIColor blackColor];
        _leftMaskView.alpha = 0.6;
        [self addSubview:_leftMaskView];
    }
    return _leftMaskView;
}
- (UIView *)rightMaskView{
    if (!_rightMaskView) {
        _rightMaskView = [[UIView alloc]init];
        _rightMaskView.backgroundColor = [UIColor blackColor];
        _rightMaskView.alpha = 0.6;
        [self addSubview:_rightMaskView];
    }
    return _rightMaskView;
}
- (UIView *)topLine{
    if (!_topLine) {
        _topLine = [[UIView alloc]init];
        _topLine.backgroundColor = [UIColor whiteColor];
        [self addSubview:_topLine];
    }
    return _topLine;
}
- (UIView *)botLine{
    if (!_botLine) {
        _botLine = [[UIView alloc]init];
        _botLine.backgroundColor = [UIColor whiteColor];
        [self addSubview:_botLine];
    }
    return _botLine;
}

#pragma mark - pan Ges
- (void)leftPanGesture:(UIPanGestureRecognizer *)pan{
    CGPoint point = [pan locationInView:self];
//    NSLog(@"left:%f",point.x);
    
    if (pan.state == UIGestureRecognizerStateBegan) {

    }else if (pan.state == UIGestureRecognizerStateChanged){
        if (point.x < DragViewWidth/2  || point.x >self.rightDragView.left - 50 ) {
            return;
        }
        
        self.leftDragView.centerX = point.x;
        if (self.leftDragView.left <= 0) {
            self.leftDragView.left = 0;
        }
        if (self.leftDragView.right >= self.width) {
            self.leftDragView.right = self.width;
        }
        
//        if (point.x > 0 && point.x < self.rightDragView.left - 50) {
//            self.leftDragView.left = point.x;

//        }else if (point.x <= 0){
//            self.leftDragView.left = 0;
//            self.leftMaskView.width = 0;
//        }
        
        //mask
        if (self.leftDragView.left >= DragViewWidth) {
            self.leftMaskView.left = DragViewWidth;
            self.leftMaskView.width = self.leftDragView.left - DragViewWidth;
        }else{
            self.leftMaskView.left = DragViewWidth;
            self.leftMaskView.width = 0;
        }
        
        self.topLine.left = self.leftDragView.left;
        self.botLine.left = self.leftDragView.left;
        self.topLine.width = self.rightDragView.right - self.leftDragView.left;
        self.botLine.width = self.rightDragView.right - self.leftDragView.left;
     }else if (pan.state == UIGestureRecognizerStateEnded){
         [self panEnded];
    }
    
}
- (void)rightPanGesture:(UIPanGestureRecognizer *)pan{
    CGPoint point = [pan locationInView:self];
//    NSLog(@"right:%f",point.x);
    if (pan.state == UIGestureRecognizerStateBegan) {
        
    }else if (pan.state == UIGestureRecognizerStateChanged){
        if (point.x < self.leftDragView.right + 50 || point.x > self.width + DragViewWidth/2 ) {
            return;
        }
        self.rightDragView.centerX = point.x;
        if (self.rightDragView.left <= 0) {
            self.rightDragView.left = 0;
        }
        if (self.rightDragView.right >= self.width) {
            self.rightDragView.right = self.width;
        }
        
//        if (point.x > self.leftDragView.right + 50 && point.x < self.width  - DragViewWidth ) {
//            self.rightDragView.left = point.x;
//
//        }else if (point.x >= self.width){
//            self.rightDragView.left = self.width - DragViewWidth;
//            self.rightMaskView.width = 0;
//        }
        
        //mask
        if (self.rightDragView.right < self.width  - DragViewWidth ) {
            self.rightMaskView.left = self.rightDragView.right;
            self.rightMaskView.width = self.width - self.rightDragView.right - DragViewWidth;
        }else{
            self.rightMaskView.left = self.rightDragView.right;
            self.rightMaskView.width = 0;
        }
        
       //line
        self.topLine.width = self.rightDragView.right - self.leftDragView.left;
        self.botLine.width = self.rightDragView.right - self.leftDragView.left;
    }else if (pan.state == UIGestureRecognizerStateEnded){
         [self panEnded];
    }
    
}

- (void)panEnded{
    CGFloat realWidth = self.width - 2 *DragViewWidth;
    CGFloat startP = self.leftDragView.right/realWidth;
    CGFloat endP = self.rightDragView.left/realWidth;
    CGFloat durationP = (self.rightDragView.left - self.leftDragView.right)/realWidth;
    if (self.cutDelegate && [self.cutDelegate respondsToSelector:@selector(videoCutoutWithStartPercent:endPercent:durationPercent:)]) {
        [self.cutDelegate videoCutoutWithStartPercent:startP endPercent:endP durationPercent:durationP];
    }
    
}

@end
